package com.kfm.shop.system.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Jwt 工具类
 * 用于生成和校验 token
 */
public class JwtUtil {
    private static final Logger log = LoggerFactory.getLogger(JwtUtil.class);

    // 过期时间
    private static final long EXPIRE_TIME = SpringContextUtil.getProperty("jwt.expirationMs", Long.class);

    private static final String SECRET = SpringContextUtil.getProperty("jwt.secret");

    /**
     * 生成签名
     *
     * @param obj
     * @return 加密的token
     */
    public static String sign(String obj) {
        return sign(obj, SECRET);
    }

    /**
     * 生成签名
     *
     * @param obj
     * @param secret  密钥
     * @return 加密的token
     */
    public static String sign(String obj, String secret) {
        if (secret == null) {
            secret = SECRET;
        }
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        Algorithm algorithm = Algorithm.HMAC256(secret);

        return JWT.create().withSubject(obj)
//                .withClaim("username", username)
                .withExpiresAt(date)
                .sign(algorithm);
    }

    /**
     * 校验token是否正确
     *
     * @param token
     * @return 是否正确
     */
    public static boolean verify(String token) {
        return verify(token, SECRET);
    }

    /**
     * 校验token是否正确
     *
     * @param token
     * @return 是否正确
     */
    public static boolean verify(String token, String secret) throws JwtException, TokenExpiredException {
        Algorithm algorithm = Algorithm.HMAC256(secret);
        JWTVerifier verifier = JWT.require(algorithm)
//                .withClaim("username", username)
                .build();
        DecodedJWT jwt = verifier.verify(token);
        return true;
    }

    /**
     * 获得token中的信息无需secret解密也能获得
     * @param token
     * @return
     */
    public static String getSubject(String token) {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getSubject();
    }

    /**
     * 获得token中的信息无需secret解密也能获得
     * @return token中包含的用户名
     */
    public static String getUsername(String token) {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim("username").asString();
    }

    public static class JwtException extends RuntimeException{
        public JwtException(String message) {
            super(message);
        }
    }

    public static void main(String[] args) {
        String sign = sign("{\"username\":\"test\"}");
        System.out.println(sign);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(verify(sign));
        String subject = getSubject(sign);
        System.out.println(subject);
    }

}
